PyTorch Image Transformations¶

  • In this notebook, I explored various image transformations using PyTorch's torchvision.transforms.
  • The following transformations were applied to an Image of a Beautiful Cat
  1. Resize: Resizing the image to a specified size.
  2. Gray Scale: Converting the image to grayscale.
  3. Normalize: Normalizing pixel values to have zero mean and unit variance.
  4. Random Rotation: Applying a random rotation to the image.
  5. Center Crop: Center-cropping the image.
  6. Random Crop: Applying a random crop to the image
  • The Image used is this one with JPEG format,size :(4288, 2848) and RGB model.

    Alt Text
In [ ]:
from PIL import Image
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import sys
import torch
import numpy as np
import torchvision.transforms as transforms
  • Resize
  • Gray Scale
  • Normalize
  • Random Rotation
  • Center Crop
  • Random Crop
In [ ]:
# Open the image file
img = Image.open("Data/cat.jpg")

# Display some information about the image
print(img.format)
print(img.size)
print(img.mode)

# Show the image 
img.show()
JPEG
(4288, 2848)
RGB
In [ ]:
# Define function to show image
def Show_Image(Image, Picture_Name):
    plt.imshow(Image)
    plt.title(Picture_Name)
    plt.show()
In [ ]:
# Resize Transformation
Resize_Transformation = transforms.Compose([
   transforms.ToPILImage(), 
   transforms.Resize(size=(850,850)),
])


# View rsult
Resized_Img = Resize_Transformation(img)
Show_Image(img, 'Original Image')
Show_Image(Resized_Img, 'Resized Image')
In [ ]:
# Horizontal Flipping
Horizontal_Flipping_Transformation = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomHorizontalFlip(p=1) 
])

# View result
Flipping_Img = Horizontal_Flipping_Transformation(img)
Show_Image(img, 'Original Image')
Show_Image(Flipping_Img, 'Flipped Image')
In [ ]:
# Vertical Flipping
Vertical_Flipping_Transformation = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomVerticalFlip(p=1) 
])

# View Result
Flipping_Img = Vertical_Flipping_Transformation(img)
Show_Image(img, 'Original Image')
Show_Image(Flipping_Img, 'Flipped Image')
In [ ]:
# Color Transformation

Color_Transformation = transforms.Compose([
    transforms.ToPILImage(),
    transforms.ColorJitter(brightness=(0.6,0.6), contrast=1,saturation=1, hue=0.4)
])

# View results.
Transformed_Img = Color_Transformation(img)
Show_Image(img, 'Original Image')
Show_Image(Transformed_Img, 'Transformed Image')
In [ ]:
# Random Crop
Crop_Transformation = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomCrop(size=(2304,1090))
])

# View output.
Cropped_Img = Crop_Transformation(img)
Show_Image(img, 'Original Image')
Show_Image(Cropped_Img, 'Cropped Image')
In [ ]:
# Rotation
Rotate_Transformation = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomRotation(degrees=90)
])

# View results
Rotated_Img = Rotate_Transformation(img)
Show_Image(img, 'Original Image')
Show_Image(Rotated_Img, 'Rotated Image')
In [ ]:
# Normalize Transform 
Normalize_Transformation = transforms.Compose([
    transforms.ToPILImage(),
    transforms.ToTensor(),
    transforms.Normalize(mean=[5,0.9,0.6], std=[1.45,0.21,7.12]), 
    transforms.ToPILImage()
])

# Testing The Transformation...
Normalized_Img = Normalize_Transformation(img)
Show_Image(img, 'Original Image')
Show_Image(Normalized_Img, 'Normalized Image')